define(["drawable"], function (Drawable) {

    Block.prototype = new Drawable();

    function Block(spriteArray) {

        this.hitPoints = 1;
        this.score = 10;
        this.isDirty = true;
        this.isVisible = true;
        this.isUnbreakable = false;
        this.sprites = spriteArray;
        this.currentFrame = 0;


        this.updateValues = function(){
            var sprite = this.currentSprite();
            this.isDirty = true;
            this.w = sprite.width;
            this.h = sprite.height;
        };
        
        this.currentSprite = function () {
            if(this.sprites !== null && this.currentFrame < this.sprites.length){
                return this.sprites[this.currentFrame];    
            }
            return null;
        };

            
        this.nextFrame = function () {
            if ( this.sprites !== null && this.currentFrame < this.sprites.length - 1) {
                this.currentFrame++;
                this.updateValues();
            }
        };

        this.previousFrame = function () {
            if (this.currentFrame > 0) {
                this.currentFrame--;
                this.updateValues();
            }
        };

        this.setFrame = function (f) {
            if (this.sprites !== null && (f >= 0) && (f <= this.sprites.length)) {
                this.currentFrame = f;
            }
        };

        if (this.sprites !== null) {            
            var s = this.currentSprite();
            this.init(s.width, s.height);
        }

        
    }


    Block.prototype.collides = function (collisionObject) {

        if (this.isUnbreakable) {
            return;
        }

        this.hitPoints--;
        this.nextFrame();
        if (this.hitPoints <= 0) {
            this.isVisible = false;
            this.isDirty = true;
        }
    };

    Block.prototype.draw = function (context) {

        this.isCollidable = this.isVisible;
        if (this.isDirty) {
            this.isDirty = false;

            context.clearRect(this.x, this.y, this.w, this.h);
            var sprite = this.currentSprite();

            if (this.isVisible && sprite !== null) {
                /*
                context.beginPath();
                context.strokeWidth = 1;
                context.strokeStyle = "#ff0000";
                context.rect(this.x, this.y, this.w, this.h);
                context.stroke();
                */
                context.drawImage(sprite, this.x, this.y);
            }
        }
    };

    return Block;
});